package com.meitianhui.convenitenbuy.util;

import android.support.annotation.NonNull;
import android.view.View;
import android.widget.AdapterView;

import com.jakewharton.rxbinding.widget.AdapterViewItemClickEvent;
import com.meitianhui.convenitenbuy.base.BaseView;
import com.meitianhui.convenitenbuy.config.LViewClickOnSubscribe;
import com.meitianhui.convenitenbuy.util.lifecycle.ActivityLifecycleable;
import com.meitianhui.convenitenbuy.util.lifecycle.FragmentLifecycleable;
import com.meitianhui.convenitenbuy.util.lifecycle.Lifecycleable;
import com.trello.rxlifecycle2.LifecycleTransformer;
import com.trello.rxlifecycle2.android.RxLifecycleAndroid;
import com.trello.rxlifecycle2.internal.Preconditions;

import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

import static com.jakewharton.rxbinding.widget.RxAdapterView.itemClickEvents;

/**
 * RxUtils 关于 rx
 */
public class RxUtils {

    // 默认按钮防手抖时间(ms)
    public static final long THROTTLE_FIRST = 500;

    public static rx.Subscription bindEvents(View view, Action1<View> action) {
        return clicks(view).throttleFirst(THROTTLE_FIRST, TimeUnit.MILLISECONDS).subscribe(action);
    }

    public static rx.Subscription bindEvents(View view) {
        return clicks(view).throttleFirst(THROTTLE_FIRST, TimeUnit.MILLISECONDS).subscribe(new Action1<View>() {
            @Override
            public void call(View view) {

            }
        });
    }


    public static  rx.Subscription bindEvents(AdapterView view, Action1<AdapterViewItemClickEvent> action){
        return itemClickEvents(view).throttleFirst(THROTTLE_FIRST, TimeUnit.MILLISECONDS).subscribe(action);
    }

    private static Observable<View> clicks(@NonNull View view){
        return Observable.create(new LViewClickOnSubscribe(view));
    }
    /**
     * 统一线程处理
     *
     * @param <T>
     * @return
     */
    public static <T> Observable.Transformer<T, T> rxSchedulerHelper() {    //compose简化线程
        return observable -> observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }
    /**
     * 绑定 Activity/Fragment 的生命周期
     * @param view
     * @param <T>
     * @return
     */
    public static <T> LifecycleTransformer<T> bindToLifecycle(BaseView view) {
        Preconditions.checkNotNull(view, "view == null");
        if (view instanceof Lifecycleable) {
            return bindToLifecycle((Lifecycleable) view);
        } else {
            throw new IllegalArgumentException("view isn't Lifecycleable");
        }
    }

    public static <T> LifecycleTransformer<T> bindToLifecycle(@NonNull Lifecycleable lifecycleable) {
        Preconditions.checkNotNull(lifecycleable, "lifecycleable == null");
        if (lifecycleable instanceof ActivityLifecycleable) {
            return RxLifecycleAndroid.bindActivity(((ActivityLifecycleable) lifecycleable).provideLifecycleSubject());
        } else if (lifecycleable instanceof FragmentLifecycleable) {
            return RxLifecycleAndroid.bindFragment(((FragmentLifecycleable) lifecycleable).provideLifecycleSubject());
        } else {
            throw new IllegalArgumentException("Lifecycleable not match");
        }
    }
}
